Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 527)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.92523 12.92697 12.92874 12.93052 12.93230 12.93407 12.93582 12.93755
## [9] 12.93923 12.94087 12.94245 12.94396 12.94539 12.94673 12.94797 12.94910
## [17] 12.95012 12.95100 12.95175 12.95234 12.95278 12.95305 12.95317 12.95320
## [25] 12.95313 12.95297 12.95272 12.95238 12.95197 12.95149 12.95094 12.95033
## [33] 12.94965 12.94893 12.94815 12.94733 12.94647 12.94558 12.94465 12.94370
## [41] 12.94273 12.94175 12.94075 12.93975 12.93874 12.93774 12.93674 12.93576
## [49] 12.93480 12.93386 12.93285 12.93170 12.93041 12.92899 12.92746 12.92581
## [57] 12.92408 12.92225 12.92036 12.91839 12.91637 12.91431 12.91221 12.91009
## [65] 12.90795 12.90581 12.90368 12.90156 12.89948 12.89742 12.89542 12.89348
## [73] 12.89160 12.88980 12.88809 12.88649 12.88499 12.88361 12.88189 12.87941
## [81] 12.87623 12.87240 12.86798 12.86304 12.85762 12.85179 12.84560 12.83912
## [89] 12.83240 12.82550 12.81848 12.81139 12.80430 12.79726 12.79034 12.78358
## [97] 12.77705 12.77080 12.76490 12.75940 12.75437 12.74985 12.74590 12.74259
## [105] 12.73998 12.73812 12.73637 12.73411 12.73138 12.72827 12.72481 12.72109
## [113] 12.71714 12.71304 12.70885 12.70463 12.70043 12.69632 12.69236 12.68861
## [121] 12.68513 12.68198 12.67921 12.67690 12.67510 12.67388 12.67328 12.67338
## [129] 12.67424 12.67590 12.67844 12.68192 12.68639 12.69279 12.70176 12.71292
## [137] 12.72589 12.74030 12.75576 12.77190 12.78834 12.80471 12.82062 12.83569
## [145] 12.84956 12.86183 12.87214 12.88245 12.89475 12.90872 12.92403 12.94035
## [153] 12.95736 12.97471 12.99210 13.00919 13.02564 13.04114 13.05535 13.06795
## [161] 13.07860 13.09031 13.10585 13.12455 13.14575 13.16880 13.19303 13.21778
## [169] 13.24240 13.26621 13.28857 13.30882 13.32628 13.34030 13.35022 13.35848
## [177] 13.36783 13.37811 13.38916 13.40081 13.41290 13.42526 13.43773 13.45014
## [185] 13.46233 13.47413 13.48537 13.49590 13.50555 13.51415 13.52155 13.52756
## [193] 13.53203 13.53480 13.53569 13.53455 13.53121 13.52551 13.51739 13.50709
## [201] 13.49483 13.48087 13.46543 13.44874 13.43106 13.41261 13.39364 13.37437
## [209] 13.35504 13.33590 13.31717 13.29910 13.27927 13.25548 13.22829 13.19828
## [217] 13.16601 13.13204 13.09694 13.06128 13.02563 12.99055 12.95660 12.92436
## [225] 12.89439 12.86725 12.83878 12.80501 12.76683 12.72513 12.68081 12.63475
## [233] 12.58785 12.54100 12.49508 12.45100 12.40963 12.37187 12.33862 12.31076
## [241] 12.28514 12.25823 12.23037 12.20188 12.17309 12.14432 12.11591 12.08818
## [249] 12.06147 12.03609 12.01238 11.99066 11.97126 11.95452 11.94065 11.92945
## [257] 11.92059 11.91373 11.90853 11.90467 11.90182 11.89963 11.89778 11.89594
## [265] 11.89376 11.89093 11.88710 11.88194 11.87799 11.87763 11.88026 11.88531
## [273] 11.89220 11.90035 11.90918 11.91811 11.92655 11.93394 11.93969 11.94322
## [281] 11.94395 11.94130 11.93812 11.93736 11.93860 11.94142 11.94539 11.95009
## [289] 11.95508 11.95996 11.96429 11.96764 11.96960 11.96974 11.96763 11.96285
## [297] 11.95643 11.94969 11.94264 11.93529 11.92765 11.91973 11.91155 11.90312
## [305] 11.89445 11.88555 11.87644 11.86712 11.85760 11.84791 11.83583 11.81960
## [313] 11.79988 11.77733 11.75262 11.72641 11.69937 11.67214 11.64541 11.61983
## [321] 11.59607 11.57478 11.55664 11.54230 11.52771 11.50889 11.48662 11.46170
## [329] 11.43491 11.40706 11.37893 11.35130 11.32497 11.30073 11.27937 11.26168
## [337] 11.24845 11.24046 11.23512 11.22941 11.22356 11.21777 11.21226 11.20724
## [345] 11.20293 11.19952 11.19725 11.19632 11.19693 11.19932 11.20367 11.21022
## [353] 11.21965 11.23223 11.24759 11.26533 11.28507 11.30641 11.32899 11.35239
## [361] 11.37625 11.40016 11.42375 11.44662 11.46839 11.48868 11.50981 11.53419
## [369] 11.56147 11.59131 11.62338 11.65735 11.69287 11.72961 11.76724 11.80541
## [377] 11.84380 11.88206 11.91987 11.95687 11.99275 12.02715 12.05976 12.09022
## [385] 12.11820 12.14738 12.18110 12.21855 12.25895 12.30148 12.34535 12.38975
## [393] 12.43387 12.47692 12.51809 12.55658 12.59159 12.62231 12.64794 12.67206
## [401] 12.69844 12.72657 12.75594 12.78604 12.81634 12.84635 12.87554 12.90342
## [409] 12.92945 12.95313 12.97396 12.99141 13.00498 13.01525 13.02331 13.02937
## [417] 13.03363 13.03632 13.03763 13.03779 13.03701 13.03549 13.03345 13.03109
## [425] 13.02864 13.02630 13.02428 13.02060 13.01343 13.00326 12.99053 12.97572
## [433] 12.95929 12.94170 12.92343 12.90493 12.88667 12.86911 12.85272 12.83797
## [441] 12.82532 12.81164 12.79395 12.77287 12.74908 12.72320 12.69589 12.66781
## [449] 12.63959 12.61188 12.58535 12.56062 12.53836 12.51920 12.50380 12.48954
## [457] 12.47357 12.45622 12.43784 12.41875 12.39930 12.37980 12.36060 12.34203
## [465] 12.32442 12.30811 12.29343 12.28071 12.27029 12.26135 12.25285 12.24479
## [473] 12.23715 12.22992 12.22311 12.21669 12.21067 12.20504 12.19978 12.19489
## [481] 12.19036 12.18619 12.18236 12.17887 12.17573 12.17294 12.17052 12.16847
## [489] 12.16680 12.16551 12.16463 12.16415 12.16408 12.16444 12.16522 12.16645
## [497] 12.16812 12.17022 12.17270 12.17557 12.17883 12.18248 12.18654 12.19099
## [505] 12.19584 12.20110 12.20676 12.21283 12.21932 12.22622 12.23353 12.24125
## [513] 12.24936 12.25786 12.26675 12.27603 12.28570 12.29577 12.30623 12.31708
## [521] 12.32833 12.33997 12.35200 12.36443 12.37725 12.39047 12.40408
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 527)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.57566 12.57357 12.57156 12.56963 12.56776 12.56595 12.56421 12.56253
## [9] 12.56091 12.55934 12.55782 12.55634 12.55492 12.55353 12.55219 12.55088
## [17] 12.54961 12.54836 12.54715 12.54595 12.54479 12.54364 12.54250 12.54138
## [25] 12.54027 12.53917 12.53808 12.53698 12.53589 12.53477 12.53362 12.53244
## [33] 12.53124 12.53002 12.52879 12.52756 12.52633 12.52511 12.52390 12.52272
## [41] 12.52156 12.52043 12.51934 12.51830 12.51731 12.51638 12.51551 12.51472
## [49] 12.51399 12.51336 12.51281 12.51235 12.51200 12.51175 12.51162 12.51161
## [57] 12.51172 12.51188 12.51198 12.51205 12.51209 12.51211 12.51213 12.51214
## [65] 12.51216 12.51221 12.51228 12.51239 12.51255 12.51277 12.51305 12.51341
## [73] 12.51386 12.51440 12.51505 12.51581 12.51670 12.51772 12.51888 12.52019
## [81] 12.52167 12.52331 12.52514 12.52716 12.52938 12.53151 12.53328 12.53471
## [89] 12.53585 12.53672 12.53735 12.53778 12.53803 12.53815 12.53815 12.53808
## [97] 12.53795 12.53781 12.53769 12.53762 12.53762 12.53774 12.53800 12.53843
## [105] 12.53906 12.53993 12.54108 12.54252 12.54429 12.54642 12.54895 12.55191
## [113] 12.55532 12.55859 12.56117 12.56316 12.56465 12.56574 12.56653 12.56711
## [121] 12.56757 12.56802 12.56855 12.56925 12.57023 12.57158 12.57339 12.57576
## [129] 12.57880 12.58259 12.58723 12.59282 12.59945 12.60826 12.62004 12.63434
## [137] 12.65073 12.66878 12.68803 12.70806 12.72843 12.74869 12.76842 12.78717
## [145] 12.80451 12.81999 12.83319 12.84728 12.86531 12.88664 12.91061 12.93658
## [153] 12.96390 12.99191 13.01997 13.04744 13.07365 13.09796 13.11972 13.13829
## [161] 13.15301 13.16728 13.18456 13.20428 13.22588 13.24879 13.27244 13.29628
## [169] 13.31973 13.34223 13.36322 13.38213 13.39838 13.41143 13.42070 13.42791
## [177] 13.43510 13.44220 13.44913 13.45581 13.46218 13.46815 13.47365 13.47860
## [185] 13.48293 13.48657 13.48943 13.49144 13.49253 13.49262 13.49163 13.48950
## [193] 13.48613 13.48147 13.47542 13.46793 13.45891 13.44828 13.43522 13.41919
## [201] 13.40059 13.37980 13.35719 13.33314 13.30805 13.28228 13.25622 13.23026
## [209] 13.20477 13.18014 13.15674 13.13496 13.11149 13.08327 13.05103 13.01553
## [217] 12.97750 12.93769 12.89685 12.85571 12.81503 12.77555 12.73801 12.70316
## [225] 12.67174 12.64449 12.61698 12.58484 12.54889 12.50997 12.46892 12.42656
## [233] 12.38373 12.34126 12.29999 12.26075 12.22438 12.19170 12.16355 12.14076
## [241] 12.12102 12.10152 12.08234 12.06357 12.04530 12.02764 12.01065 11.99445
## [249] 11.97911 11.96473 11.95139 11.93920 11.92823 11.91858 11.91155 11.90804
## [257] 11.90758 11.90970 11.91391 11.91973 11.92668 11.93429 11.94208 11.94956
## [265] 11.95627 11.96171 11.96542 11.96690 11.96974 11.97730 11.98881 12.00349
## [273] 12.02057 12.03927 12.05882 12.07844 12.09737 12.11482 12.13002 12.14220
## [281] 12.15058 12.15439 12.15737 12.16337 12.17181 12.18211 12.19369 12.20597
## [289] 12.21836 12.23027 12.24114 12.25037 12.25738 12.26160 12.26243 12.25929
## [297] 12.25224 12.24200 12.22903 12.21378 12.19670 12.17825 12.15888 12.13904
## [305] 12.11918 12.09976 12.08123 12.06405 12.04865 12.03551 12.02104 12.00189
## [313] 11.97880 11.95254 11.92386 11.89352 11.86226 11.83085 11.80003 11.77057
## [321] 11.74322 11.71874 11.69787 11.68138 11.66551 11.64643 11.62473 11.60105
## [329] 11.57598 11.55016 11.52419 11.49868 11.47425 11.45152 11.43109 11.41359
## [337] 11.39963 11.38982 11.38174 11.37278 11.36322 11.35336 11.34351 11.33396
## [345] 11.32501 11.31695 11.31009 11.30471 11.30113 11.29963 11.30052 11.30410
## [353] 11.31044 11.31926 11.33029 11.34325 11.35789 11.37393 11.39109 11.40912
## [361] 11.42773 11.44666 11.46565 11.48441 11.50269 11.52020 11.53941 11.56265
## [369] 11.58951 11.61954 11.65231 11.68741 11.72439 11.76283 11.80230 11.84236
## [377] 11.88260 11.92257 11.96185 12.00001 12.03662 12.07124 12.10346 12.13283
## [385] 12.15894 12.18482 12.21350 12.24445 12.27717 12.31115 12.34590 12.38088
## [393] 12.41561 12.44957 12.48226 12.51317 12.54178 12.56760 12.59011 12.61184
## [401] 12.63540 12.66037 12.68638 12.71303 12.73992 12.76667 12.79288 12.81815
## [409] 12.84209 12.86431 12.88441 12.90201 12.91671 12.92933 12.94100 12.95174
## [417] 12.96156 12.97049 12.97854 12.98575 12.99212 12.99769 13.00247 13.00648
## [425] 13.00975 13.01230 13.01414 13.01451 13.01276 13.00912 13.00382 12.99706
## [433] 12.98909 12.98012 12.97037 12.96007 12.94944 12.93870 12.92809 12.91781
## [441] 12.90810 12.89742 12.88432 12.86913 12.85218 12.83379 12.81431 12.79407
## [449] 12.77339 12.75261 12.73205 12.71205 12.69295 12.67506 12.65873 12.64092
## [457] 12.61887 12.59334 12.56506 12.53480 12.50328 12.47127 12.43951 12.40875
## [465] 12.37973 12.35321 12.32992 12.31063 12.29608 12.28376 12.27083 12.25746
## [473] 12.24386 12.23021 12.21671 12.20355 12.19093 12.17903 12.16805 12.15818
## [481] 12.14962 12.14255 12.13717 12.13272 12.12837 12.12420 12.12029 12.11671
## [489] 12.11355 12.11087 12.10877 12.10730 12.10657 12.10663 12.10758 12.10948
## [497] 12.11242 12.11609 12.12015 12.12465 12.12962 12.13511 12.14116 12.14780
## [505] 12.15509 12.16305 12.17173 12.18117 12.19141 12.20249 12.21445 12.22736
## [513] 12.24123 12.25603 12.27174 12.28833 12.30576 12.32400 12.34303 12.36282
## [521] 12.38334 12.40455 12.42644 12.44896 12.47209 12.49580 12.52007
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 527)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.84589 11.84993 11.85402 11.85815 11.86231 11.86647 11.87062 11.87475
## [9] 11.87884 11.88288 11.88684 11.89073 11.89451 11.89818 11.90172 11.90511
## [17] 11.90834 11.91139 11.91426 11.91691 11.91934 11.92154 11.92348 11.92515
## [25] 11.92654 11.92763 11.92840 11.92885 11.92894 11.92874 11.92830 11.92764
## [33] 11.92677 11.92570 11.92445 11.92303 11.92145 11.91974 11.91789 11.91593
## [41] 11.91387 11.91173 11.90950 11.90722 11.90490 11.90254 11.90016 11.89777
## [49] 11.89539 11.89304 11.89072 11.88844 11.88623 11.88410 11.88205 11.88011
## [57] 11.87829 11.87617 11.87336 11.86993 11.86592 11.86139 11.85638 11.85095
## [65] 11.84516 11.83905 11.83268 11.82610 11.81936 11.81252 11.80562 11.79872
## [73] 11.79188 11.78515 11.77857 11.77220 11.76609 11.76030 11.75488 11.74988
## [81] 11.74535 11.74135 11.73792 11.73513 11.73301 11.73049 11.72653 11.72125
## [89] 11.71477 11.70723 11.69875 11.68946 11.67949 11.66896 11.65800 11.64674
## [97] 11.63531 11.62383 11.61242 11.60122 11.59036 11.57996 11.57014 11.56104
## [105] 11.55278 11.54549 11.53929 11.53432 11.53070 11.52855 11.52801 11.52920
## [113] 11.53225 11.53706 11.54336 11.55105 11.56001 11.57014 11.58133 11.59346
## [121] 11.60643 11.62012 11.63442 11.64923 11.66444 11.67993 11.69559 11.71131
## [129] 11.72699 11.74252 11.75777 11.77265 11.78704 11.80360 11.82460 11.84938
## [137] 11.87727 11.90759 11.93968 11.97285 12.00644 12.03978 12.07220 12.10302
## [145] 12.13157 12.15719 12.17919 12.20124 12.22702 12.25591 12.28726 12.32045
## [153] 12.35486 12.38984 12.42477 12.45902 12.49195 12.52295 12.55136 12.57658
## [161] 12.59796 12.61818 12.64011 12.66338 12.68761 12.71246 12.73756 12.76254
## [169] 12.78704 12.81069 12.83314 12.85402 12.87296 12.88961 12.90359 12.91637
## [177] 12.92954 12.94300 12.95663 12.97031 12.98393 12.99736 13.01049 13.02321
## [185] 13.03539 13.04692 13.05768 13.06755 13.07642 13.08417 13.09068 13.09584
## [193] 13.09952 13.10162 13.10201 13.10057 13.09720 13.09177 13.08440 13.07540
## [201] 13.06485 13.05287 13.03957 13.02506 13.00944 12.99283 12.97533 12.95705
## [209] 12.93810 12.91859 12.89863 12.87832 12.85455 12.82474 12.78984 12.75076
## [217] 12.70844 12.66382 12.61781 12.57136 12.52539 12.48084 12.43864 12.39971
## [225] 12.36499 12.33541 12.30564 12.27034 12.23050 12.18709 12.14108 12.09344
## [233] 12.04515 11.99716 11.95046 11.90602 11.86480 11.82779 11.79594 11.77024
## [241] 11.74745 11.72386 11.69972 11.67526 11.65074 11.62639 11.60246 11.57920
## [249] 11.55685 11.53566 11.51586 11.49770 11.48144 11.46730 11.45598 11.44773
## [257] 11.44215 11.43883 11.43736 11.43735 11.43839 11.44007 11.44200 11.44378
## [265] 11.44498 11.44522 11.44409 11.44119 11.44056 11.44587 11.45615 11.47044
## [273] 11.48777 11.50718 11.52770 11.54836 11.56820 11.58625 11.60155 11.61312
## [281] 11.62001 11.62124 11.62111 11.62415 11.62976 11.63734 11.64630 11.65603
## [289] 11.66594 11.67543 11.68390 11.69075 11.69539 11.69721 11.69562 11.69002
## [297] 11.67899 11.66213 11.64037 11.61464 11.58586 11.55495 11.52285 11.49046
## [305] 11.45873 11.42858 11.40092 11.37669 11.35681 11.34221 11.32751 11.30735
## [313] 11.28266 11.25438 11.22344 11.19077 11.15731 11.12399 11.09174 11.06151
## [321] 11.03422 11.01080 10.99220 10.97934 10.96952 10.95951 10.94946 10.93954
## [329] 10.92989 10.92067 10.91204 10.90415 10.89716 10.89123 10.88650 10.88314
## [337] 10.88130 10.88114 10.88278 10.88614 10.89107 10.89740 10.90498 10.91367
## [345] 10.92329 10.93369 10.94472 10.95623 10.96804 10.98002 10.99199 11.00382
## [353] 11.01719 11.03362 11.05271 11.07403 11.09716 11.12169 11.14719 11.17324
## [361] 11.19944 11.22535 11.25056 11.27464 11.29719 11.31779 11.33837 11.36104
## [369] 11.38555 11.41169 11.43923 11.46794 11.49758 11.52795 11.55880 11.58991
## [377] 11.62105 11.65200 11.68253 11.71242 11.74142 11.76933 11.79591 11.82093
## [385] 11.84417 11.86735 11.89214 11.91824 11.94532 11.97307 12.00118 12.02933
## [393] 12.05721 12.08450 12.11089 12.13606 12.15971 12.18151 12.20115 12.22097
## [401] 12.24318 12.26732 12.29289 12.31944 12.34647 12.37351 12.40009 12.42572
## [409] 12.44994 12.47225 12.49219 12.50928 12.52304 12.53449 12.54500 12.55459
## [417] 12.56329 12.57115 12.57818 12.58442 12.58991 12.59467 12.59874 12.60214
## [425] 12.60491 12.60709 12.60870 12.60864 12.60604 12.60122 12.59451 12.58622
## [433] 12.57671 12.56628 12.55527 12.54400 12.53281 12.52202 12.51197 12.50297
## [441] 12.49536 12.48712 12.47628 12.46324 12.44839 12.43212 12.41484 12.39693
## [449] 12.37879 12.36081 12.34340 12.32693 12.31182 12.29845 12.28721 12.27646
## [457] 12.26443 12.25134 12.23742 12.22288 12.20794 12.19284 12.17778 12.16299
## [465] 12.14870 12.13511 12.12247 12.11098 12.10087 12.09166 12.08273 12.07403
## [473] 12.06554 12.05721 12.04903 12.04095 12.03295 12.02499 12.01703 12.00906
## [481] 12.00102 11.99290 11.98466 11.97639 11.96819 11.96008 11.95204 11.94408
## [489] 11.93618 11.92835 11.92059 11.91288 11.90523 11.89764 11.89010 11.88262
## [497] 11.87517 11.86778 11.86043 11.85313 11.84588 11.83869 11.83156 11.82449
## [505] 11.81748 11.81054 11.80366 11.79686 11.79013 11.78347 11.77689 11.77037
## [513] 11.76389 11.75744 11.75104 11.74468 11.73837 11.73210 11.72589 11.71973
## [521] 11.71362 11.70757 11.70158 11.69564 11.68977 11.68397 11.67823
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")